package com.spark.service;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class WebShowService {
    static String url="jdbc:mysql://node3:3306/test";
    static String username="root";
    static String password="123";

    /**
     * 查询标题名字，标题点击个数
     * @return
     */
    public Map<String,Object> queryWebLogs(){
        Connection conn=null;
        PreparedStatement pst=null;
        String[] titleNames=new String[30];
        String[] titleCounts=new String[30];
        Map<String,Object> retMap=new HashMap<String, Object>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            String query_sql="select titleName,count from webCount where 1=1 order by count desc limit 30";
            pst=conn.prepareStatement(query_sql);
            ResultSet rs=pst.executeQuery();
            int i=0;
            while (rs.next()){
                String tilleName=rs.getString("titleName");
                String titleCount=rs.getString("count");
                titleNames[i]=tilleName;
                titleCounts[i]=titleCount;
                ++i;
            }
            retMap.put("titleName",titleNames);
            retMap.put("titleCount",titleCounts);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(pst!=null){
                    pst.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return retMap;
    }
    //查询多少条
    public String[] titleCount() {
        String[] titleSums = new String[1];
        Connection conn=null;
        PreparedStatement pst=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,username,password);
            String query_sql = "select count(1) titleSum from webCount";
            pst = conn.prepareStatement(query_sql);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                String titleSum = rs.getString("titleSum");
                titleSums[0] = titleSum;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        return titleSums;
    }
}
